package com.ljx.recruitment.sale.service.impl.token;

import cn.hutool.json.JSONUtil;
import com.ljx.recruitment.sale.domain.dto.TrialApplicationTokenParamsDto;
import com.ljx.recruitment.sale.domain.entity.AccessToken;
import com.ljx.recruitment.sale.enums.AccessTokenTypeEnum;
import com.ljx.recruitment.sale.service.AccessTokenStrategy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class TrialApplicationAccessTokenStrategy implements AccessTokenStrategy {

    @Value("${recruitment.url.front}")
    private String frontUrl;

    @Override
    public Integer getType() {
        return AccessTokenTypeEnum.TRIAL_APPLICATION.getKey();
    }

    @Override
    public String parseToken(AccessToken accessToken) {
        TrialApplicationTokenParamsDto paramsDto = JSONUtil.toBean(accessToken.getTokenParams(), TrialApplicationTokenParamsDto.class);
        return UriComponentsBuilder.fromHttpUrl(frontUrl + "/share")
                .queryParam("type", AccessTokenTypeEnum.cache.get(accessToken.getTokenType()).getValue())
                .queryParam("trialApplicationId", paramsDto.getTrialApplicationId())
                .queryParam("from", accessToken.getUserId())
                .queryParam("at", accessToken.getToken())
                .toUriString();
    }
}
